﻿using Android.Accessibilityservice.AccessibilityService;
using Android.Content;
using Android.Graphics;
using Android.Hardware.Display;
using Android.Icu.Text;
using Android.Media;
using Android.Media.Projection;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Java.Nio;

namespace ScreenShotDemo
{
    [Service(ForegroundServiceType = Android.Content.PM.ForegroundService.TypeMediaProjection)]
    public class MyMediaProjectionService: Service
    {
        public static MyMediaProjectionService? _instance { get; private set; }
        public static CaptureScreenHelper? _captureScreenHelper { get; private set; }
        public override IBinder? OnBind(Intent? intent) => null;
        public override void OnCreate()
        {
            base.OnCreate();
            // Initialize your service here
        }
        public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFlags flags, int startId)
        {
            // 初始化MediaProjection
            var resultCode = intent.GetIntExtra("resultCode", (int)Result.Canceled);
            var data = intent.GetParcelableExtra("data") as Intent;
            _captureScreenHelper = CaptureScreenHelper.GetCaptureScreenHelper(this, resultCode, data);
            return StartCommandResult.Sticky;
        }
        

        public override void OnDestroy()
        {
            base.OnDestroy();
            // Clean up resources here
            if (_captureScreenHelper != null)
            {
                _captureScreenHelper.Dispose();
                _captureScreenHelper = null;
            }
        }

    }
}
